import os

def rename_files_recursively(directory, suffix_to_remove):
    """
    递归批量重命名指定文件夹及其子文件夹中的文件，移除指定的文件名后缀。

    参数:
    directory (str): 文件夹路径。
    suffix_to_remove (str): 需要去除的文件名后缀。
    """
    try:
        # 遍历文件夹及其所有子文件夹
        for root, dirs, files in os.walk(directory):
            for filename in files:
                # 分离文件名和扩展名
                base_name, extension = os.path.splitext(filename)

                # 检查文件名是否包含需要移除的后缀
                if base_name.endswith(suffix_to_remove):
                    # 移除指定的后缀
                    new_base_name = base_name[:-len(suffix_to_remove)]

                    # 构建新的文件名
                    new_filename = f"{new_base_name}{extension}"

                    # 构建完整的旧文件路径和新文件路径
                    old_file_path = os.path.join(root, filename)
                    new_file_path = os.path.join(root, new_filename)

                    # 重命名文件
                    os.rename(old_file_path, new_file_path)
                    print(f'Renamed "{filename}" to "{new_filename}"')

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    # 指定文件夹路径
    directory = r"E:\妙思维JS++WEB前端高级工程师"
    # 指定需要移除的文件名后缀
    suffix_to_remove = "[天下无鱼][shikey.com]"

    rename_files_recursively(directory, suffix_to_remove)